package cn.avalon.user.controller;

import cn.avalon.basic.exception.BusinessException;
import cn.avalon.basic.utils.AjaxResult;
import cn.avalon.basic.utils.PageList;
import cn.avalon.user.domain.User;
import cn.avalon.user.dto.UserDto;
import cn.avalon.user.query.UserQuery;
import cn.avalon.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @description: 后端接口类；
 * @author: Avalon
 * @date: 2022年08月24日 ： 19:21
 * @version: ver1.0
 * @email 709785494@qq.com
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public UserService userService;

    /**
     * 接口：添加或修改
     * @param user  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.save(user);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：删除
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value="/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            userService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     * @param id
     */
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id")Long id) {
        return userService.findById(id);
    }


    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    public List<User> findAll(){
        return userService.findAll();
    }

    /**
     * 接口：分页查询或高级查询
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }

    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto) {
        try {
            userService.phoneRegister(userDto);
            return new AjaxResult();
            //只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"注册失败");
        }
    }

    /**
     * 邮箱注册
     * @param userDto
     * @return
     */
    @PostMapping("/register/email")
    public AjaxResult registerEmail(@RequestBody UserDto userDto) {
        try {
            userService.registerEmail(userDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试");
        }
    }

    /**
     * 激活邮箱
     * @param id
     * @param res
     */
    @GetMapping("/register/email/activate/{id}")
    public void emailActivate(@PathVariable("id") Long id, HttpServletResponse res) {
        res.setContentType("text/html;charset=UTF-8");
        try {
            userService.emailActivate(id);
            PrintWriter writer = res.getWriter();
            writer.println("恭喜你激活成功");
            writer.println("<a href='http://localhost/login.html'>点击跳转登录页面</a>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
